package com.justrun.zjutsystem.zjutsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.justrun.response.ResultCodeEnum;
import com.justrun.response.ResultEntity;
import com.justrun.zjutsystem.zjutsystem.dtos.CollegeClasssDto;
import com.justrun.zjutsystem.zjutsystem.entity.AdministrativeClass;
import com.justrun.zjutsystem.zjutsystem.entity.College;
import com.justrun.zjutsystem.zjutsystem.mapper.AdministrativeClassMapper;
import com.justrun.zjutsystem.zjutsystem.mapper.CollegeMapper;
import com.justrun.zjutsystem.zjutsystem.service.CollegeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.justrun.zjutsystem.zjutsystem.vo.CollegeSelectorVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学院信息表 服务实现类
 * </p>
 *
 * @author chandler
 * @since 2020-09-17
 */
@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeMapper, College> implements CollegeService {

    @Autowired
    private CollegeMapper collegeMapper;
    @Autowired
    private AdministrativeClassMapper administrativeClassMapper;
    @Override
    public ResultEntity getCollegeList(String collegeName,Integer pageIndex, Integer pageSize) {
        Page<College> page = new Page<>(pageIndex,pageSize);
        Page<College> resultPage = null;
        //如若学院查询条件不为空
        if (!StringUtils.isEmpty(collegeName)){
            College college = new College();
            college.setCollegeName(collegeName);
            QueryWrapper queryWrapper = new QueryWrapper(college);
            resultPage = collegeMapper.selectPage(page,queryWrapper);
        }else {
            resultPage = collegeMapper.selectPage(page,null);
        }
        long total = resultPage.getTotal();
        List<College> collegeRecords = resultPage.getRecords();
        List<CollegeClasssDto> collegeClasssDtos = new ArrayList<>();
        //遍历每个学院
        for (College college1:collegeRecords){
            //根据学院编号查询学院的所有班级
            QueryWrapper<AdministrativeClass> wrapper = new QueryWrapper<>();
            wrapper.eq("college_id",college1.getCollegeId());
            List<AdministrativeClass> classList = administrativeClassMapper.selectList(wrapper);
            List<String> classNames = new ArrayList<>();
            //遍历班级
            for (AdministrativeClass administrativeClass:classList){
                classNames.add(administrativeClass.getClassName());
            }
            CollegeClasssDto collegeClasssDto = new CollegeClasssDto();
            collegeClasssDto.setCollegeId(college1.getCollegeId());
            collegeClasssDto.setCollegeName(college1.getCollegeName());
            collegeClasssDto.setClassList(classNames);
            collegeClasssDtos.add(collegeClasssDto);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("totalCount",total);
        resultMap.put("currentPage",resultPage.getCurrent());
        resultMap.put("pageSize",resultPage.getSize());
        resultMap.put("rows",collegeClasssDtos);
        return ResultEntity.ok().data(resultMap);
    }

    @Override
    public ResultEntity getCollegeNames() {
        List<String> names = collegeMapper.findAllCollegeNames();
        List<CollegeSelectorVo> selectorVoList = new ArrayList<>();
        for (String name:names) {
            CollegeSelectorVo vo = new CollegeSelectorVo(name,name);
            selectorVoList.add(vo);
        }
        return ResultEntity.ok().data(selectorVoList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultEntity batchAdd(List<List<String>> recordList) {
        for(List<String> record:recordList){
            //每一条记录必须包括班级编号、班级名称、学员编号和学院名称
            if (record.size() != 4){
                return ResultEntity.fail().code(ResultCodeEnum.FILE_CONTENT_ERROR.getCode())
                        .message(ResultCodeEnum.FILE_CONTENT_ERROR.getMesssage());
            }
            //查询学院表中是否存在该学院
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("college_id",record.get(2));
            if (collegeMapper.selectOne(queryWrapper) == null) {
                //没有则先插入学院
                College college = new College();
                college.setCollegeId(record.get(2));
                college.setCollegeName(record.get(3));
                collegeMapper.insert(college);
            }
            //然后插入班级
            AdministrativeClass administrativeClass = new AdministrativeClass();
            administrativeClass.setCollegeId(record.get(2));
            administrativeClass.setCollegeName(record.get(3));
            administrativeClass.setClassId(record.get(0));
            administrativeClass.setClassName(record.get(1));
            administrativeClassMapper.insert(administrativeClass);
        }
        return ResultEntity.ok();
    }
}
